// pages/Tabbar/User/User.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { getUser } from '../../../api/user';
import { getGradeConfig } from '../../../api/brand';
import { queryPetInfo } from '../../../api/pet';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    CustomBar: app.globalData.CustomBar,
    statusBarHeight: wx.getSystemInfoSync().statusBarHeight,
    tabbarHeight: wx.getSystemInfoSync().screenHeight - wx.getSystemInfoSync().safeArea.bottom + 60,
    hasSelUser: 0,
    user: {
      nickName: '',
      phone: '',
    },
    gradeList: [],
    petList: [], // 宠物列表
    appList: [
      { label: '旅游订单' , icon: `${assetsAddress}/user/travelOrderIcon.png`, path: '/pages/Order/TravelOrder/TravelOrder'},
      { label: '用户协议' , icon: `${assetsAddress}/user/agreementIcon.png`, path: ''},
      { label: '联系客服' , icon: `${assetsAddress}/user/customerIcon.png`, path: '/pages/Systems/Feedbacks/Feedbacks'},
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      console.log(this.getTabBar(),'gettabbar')
      this.getTabBar().setData({
        active: 3
      })
    }
    this.getUser();
    this._getGradeConfig();
    this._queryPetInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getUser();
    this._getGradeConfig();
    setTimeout(() => {
      wx.stopPullDownRefresh({
        success: (res) => {
          console.log(res, '刷新成功')
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = wx.getStorageSync('userId') ?? 10;
    return {
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 获取用户个人信息
  getUser(){
    let _this = this;
    getUser({})
    .then(res => {
      if(res.success){
        _this.setData({
          user: res.data,
          hasSelUser: 1,
        })
        app.globalData.userInfo = res.data;
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 获取品牌等级配置信息
  _getGradeConfig(){
    let _this = this;
    getGradeConfig()
    .then(res => {
      if(res.success){
        _this.setData({
          gradeList: res.data.content
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 页面跳转
  goNext(e){
    let type = e.target.dataset.type||e.currentTarget.dataset.type;
    let url = e.target.dataset.name||e.currentTarget.dataset.name;
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    // console.log(JSON.stringify(item))
    // return
    if(item){
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}?item=${JSON.stringify(item)}`,
        })
      }
    }else{
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}`,
        })
      }
    }
  },
  // 查询宠物信息
  _queryPetInfo(){
    let _this = this;
    queryPetInfo({userId: wx.getStorageSync('userId')})
    .then(res => {
      if(res.success){
        _this.setData({
          petList: res.data.content,
        })
        console.log(_this.data.petList,'petList')
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
})